package com.ezhuan.users.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ezhuan.common.dao.impl.BaseDaoImpl;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.users.dao.IAWithdrawHistoryDao;
import com.ezhuan.users.pojo.AWithdrawHistory;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 用户表Dao的实现类
 * 
 * @author wangshSxt
 *
 */
public class AWithdrawHistoryDaoImpl extends BaseDaoImpl<AWithdrawHistory> implements IAWithdrawHistoryDao
{
	private AWithdrawHistory withdraw = new AWithdrawHistory();
	
	@Override
	public AWithdrawHistory findOneDao(Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ withdraw.getTableName() +" where 1 = 1 ");
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 按照ID查询 */
			sqlSb.append(" and id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 木有任何条件,直接返回Null */
			return null ; 
		}
		Record record = Db.findFirst(sqlSb.toString(), paramsList.toArray());
		if (record != null)
		{
			/* 用户信息 */
			AWithdrawHistory withdraw = new AWithdrawHistory();
			return withdraw.parseRecord(record);
		}
		return null;
	}

	@Override
	public List<AWithdrawHistory> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ withdraw.getTableName() +" where 1 = 1 ");
		/* 查看提现资料 withdrawId */
		if (condMap.get("withdrawId") != null && !"".equalsIgnoreCase(condMap.get("withdrawId") + ""))
		{
			sqlSb.append(" and withdrawId = ? ");
			paramsList.add(condMap.get("withdrawId"));
		}
		
		/* 查按照用户Id查询
		 * 查询用户id下面的提现资料id
		 *  */
		if (condMap.get("withdrawIdList") instanceof List )
		{
			List<String> withdrawIdList = (List<String>) condMap.get("withdrawIdList") ; 
			if(withdrawIdList.size() > 0 )
			{
				sqlSb.append(" and (");
				for (Iterator iterator = withdrawIdList.iterator(); iterator.hasNext();)
				{
					String withdrawId = (String) iterator.next();
					sqlSb.append(" withdrawId = ? or ") ; 
					paramsList.add(withdrawId);
				}
				sqlSb.append(")");
			}
		}
		
		/* 按照创建时间查询 */
		if(condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sqlSb.append(" and createTime >= ? and createTime < ? ");
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("edDate"));
		}
		
		List<Record> recordList = Collections.EMPTY_LIST ; 
		/* 分页 */
		if (pageInfoUtil != null)
		{
			recordList = this.findByPage(pageInfoUtil, sqlSb.toString(), paramsList);
		}else
		{
			recordList = this.find(sqlSb.toString(), paramsList);
		}

		/* 存储结果 */
		List<AWithdrawHistory> withdrawHistoryList = new ArrayList<AWithdrawHistory>();
		for (Iterator iterator = recordList.iterator(); iterator.hasNext();)
		{
			Record record = (Record) iterator.next();
			/* 用户信息 */
			AWithdrawHistory withdrawHistory = new AWithdrawHistory();
			withdrawHistory = withdrawHistory.parseRecord(record);
			withdrawHistoryList.add(withdrawHistory);
		}
		return withdrawHistoryList;
	}
}
